package com.gimi.cloud.bbp.merchantZone.controller.product;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.dto.product.ProductSkuDto;
import com.gimi.cloud.bbp.merchantZone.service.product.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by shazhiwei on 2017/12/25.
 */
@RestController
@RequestMapping(value = "sku")
public class SkuController {
    private final SkuService skuService;

    @Autowired
    public SkuController(SkuService skuService){
        this.skuService = skuService;
    }
    @RequestMapping(value = "/list")
    public void listSku(@RequestBody(required = false) ProductSkuDto dto, HttpServletResponse response) throws Exception {
        Response resp = new Response();
        List<ProductSkuDto> data = skuService.getSkuList(dto.getProdId());
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }
    /**
     * sku模板属性关系转化
     * @param dto
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/getSkuModelDetail")
    public void getSkuModelDetail(@RequestBody(required = false) ProductSkuDto dto, HttpServletResponse response) throws Exception {
    	Response resp = new Response();
    	List<Map<String,Object>> skuModelDetail = skuService.getSkuModelDetail(dto.getSkuValue());
    	resp.setRes_data(skuModelDetail);
    	resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
    	Util.sendResponse(JsonUtil.toJson(resp), response);
    }
    /**
     * 更新Sku属性
     * @param list
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/update")
    public void saveSku(@RequestBody List<ProductSkuDto> list, HttpServletResponse response) throws Exception {
    	Response resp = new Response();
    	skuService.saveSkuList(list);
    	resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
    	Util.sendResponse(JsonUtil.toJson(resp), response);
    }
    /**
     * 获得sku所对应商品的状态 
     * @param prodId
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/status")
    public void status(@RequestBody String prodId, HttpServletResponse response) throws Exception {
    	Response resp = new Response();
    	String status = skuService.getProdStatus(prodId);
    	resp.setRes_data(status);
    	resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
    	Util.sendResponse(JsonUtil.toJson(resp), response);
    }
    /**
     * 根据sku_id删除本条sku
     */
    @RequestMapping(value = "/deleteOne")
    public void deleteSku(@RequestBody(required = true) ProductSkuDto dto, HttpServletResponse response) throws Exception {
        Response resp = new Response();
        skuService.deleteOne(dto);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }
    /**
     * 根据sku_id查看sku图片
     */
    @RequestMapping(value = "/skuImg")
    public void getSkuImg(@RequestBody(required = true) ProductSkuDto dto, HttpServletResponse response) throws Exception {
    	Response resp = new Response();
    	List<String> list = skuService.findSkuImg(dto);
    	Map<String,Object> map = new HashMap<>();
    	map.put("imgUrl", list);
    	resp.setRes_data(map);
    	resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
    	Util.sendResponse(JsonUtil.toJson(resp), response);
    }
    
}
